<template>
  <zero-dialog :showDialog.sync="resetPasswordFormVisible"
               :loading="loading"
               :btnExist="btnExist"
               :width="'350px'"
               title="重置密码"
               :submit="resetPassword">
    <div slot="body">
      <el-form :model="resetPasswordForm"
               :rules="rules"
               label-width="50px"
               ref="resetPasswordFormRef">
        <el-form-item prop="newPassword"
                      label="密码">
          <el-input v-model.trim="resetPasswordForm.newPassword"
                    placeholder="请输入密码"
                    show-password />
        </el-form-item>
      </el-form>
    </div>
  </zero-dialog>
</template>

<script>
export default {
  name: 'ResetPasswordForm',
  props: {
    id: String,
    resetPasswordUrl: String
  },
  data () {
    return {
      loading: false,
      btnExist: true, // 按钮是否存在
      resetPasswordFormVisible: false, // 是否显示弹框
      resetPasswordForm: {},
      rules: {
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    initDialog (row) {
      this.btnExist = true
      this.fullscreen = false
      this.resetPasswordForm = {}
      this.resetPasswordFormVisible = true
      this.resetPasswordForm.userId = row.id
    },
    resetPassword () {
      this.$refs.resetPasswordFormRef.validate(valid => {
        if (valid) {
          this.btnExist = false
          this.loading = true
          const config = {
            url: this.resetPasswordUrl,
            method: 'get',
            params: {
              userId: this.resetPasswordForm.userId,
              newPassword: this.$getRsaCode(this.resetPasswordForm.newPassword)
            }
          }
          this.$axios(config)
            .then(res => {
              this.loading = false
              this.resetPasswordFormVisible = false
              this.$message.success(res.msg)
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
